<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class calcule_administrative_ctr extends CI_Controller {  

	    function __construct()
    {
        parent::__construct();
        
		$this->load->helper(array('form', 'url'));
	
        $this->load->helper('form'); 
 
    }
	  
 function index()
	{
		
		 $this->load->library('session');
		 $year=$this->session->userdata('annee');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('calcule_administrative_mdl'); // load the file welcome_mdl.php
	
	   $tab_ch=$this->etat_chapitres();
	    $nom_chapitres= $this->charticle_mdl->get_chapitres();
		$budget=$this->calcule_administrative_mdl->get_all();
		$data["chapitres"] = $tab_ch;
		
		$data['nom_chapitres']=$nom_chapitres;
		
			$data["result"] = $budget;
       		$data['titre']='الحساب الاداريلسنة: <br>'. $year;
		
		  $this->load->view('calcule_administrative_vew',$data);  // send the result from the model to the view 
		 
	
	}
	
	
	 function etat_chapitres()
	{
		
		 $this->load->library('session');
		 $year=$this->session->userdata('annee');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('calcule_administrative_mdl'); // load the file welcome_mdl.php
	
	  for($i=11;$i<20;$i++){
	  	 $prix_bc=0;
	   $prix_conv=0;
	
	  	// calcule montant des bc engager
	  	 $inf[$i] = $this->calcule_administrative_mdl->calcule_chapitre($i);
		 if($inf[$i]){
		 	$tmp=$inf[$i];
			$prix_bc=0.00;
		 	for($j=0;$j<sizeof($inf[$i]);$j++){
				$prix_bc=$prix_bc+($tmp[$j]->bc_produit_pu*$tmp[$j]->quantite);
			}
			}
			// calcule montant de convention engager
			 $info[$i] = $this->calcule_administrative_mdl->calcule_chapitre_convention($i);
		 if($info[$i]){
		 	$tmpo=$info[$i];
			$prix_conv=0.00;
		 	for($j=0;$j<sizeof($info[$i]);$j++){
				$prix_conv=$prix_conv+($tmpo[$j]->pu_convention_produit*$tmpo[$j]->quantite);
			}
			
		 }
		 
	
		 $prix=$prix_bc+$prix_conv;
		
			$tab[$i]=$prix;
		
	  }
       
$g=0;
	
		
	return $tab;	
	
	}

     function detaille_chapitre($ch){
   	$this->load->model('charticle_mdl');
   	$v=$this->input->post($ch);
	$this->load->model('calcule_administrative_mdl'); // load the file welcome_mdl.php
	
	$nom_articles=$this->charticle_mdl->get_articles($ch);

		for($i=0;$i<sizeof($nom_articles);$i++){
			$compt=$i+1;
			
			// calcule montant bc engager articles
			$tmp=$this->calcule_administrative_mdl->calcule_article($ch,$compt);
			$prix_bc=0;
			if($tmp){
				
			for($j=0;$j<sizeof($tmp);$j++){
			$prix_bc=$prix_bc+($tmp[$j]->bc_produit_pu * $tmp[$j]->quantite);	
			}
			}
			
		
			
	
		// calcule montant convention engager articles
		$tmpo=$this->calcule_administrative_mdl->calcule_article_convention($ch,$compt);
			$prix_conv=0;
			if($tmpo){
				
			for($j=0;$j<sizeof($tmpo);$j++){
			$prix_conv=$prix_conv+($tmpo[$j]->pu_convention_produit * $tmpo[$j]->quantite);	
			}
			}
			
			
				// calcule montant transfert plus articles
		
			
		
	
			
		$comsomer[$i]=$prix_bc+$prix_conv;
	
		
	}
		for($i=0;$i<sizeof($nom_articles);$i++){
			$code=$i+1;
			
				$tran_source[$code]=$this->calcule_administrative_mdl->calcule_article_transfert_source($ch,$code);
				$tran_dest[$code]=$this->calcule_administrative_mdl->calcule_article_transfert_destination($ch,$code);
			}
			
			
	$budget_init_ch=$this->calcule_administrative_mdl->get_articles_solde($ch,0);
	$budget_supl_ch=$this->calcule_administrative_mdl->get_articles_solde($ch,1);
	
	$m=0;
    
	$data['tran_dest']=$tran_dest;
	$data['tran_source']=$tran_source;
	$data['consommer']=$comsomer;
	$data['nom_art']=$nom_articles;
	$data['budget_init_ch']=$budget_init_ch;
	$data['budget_supl_ch']=$budget_supl_ch;
	$data['num_ch']=$ch;
	
	  $this->load->view('calcule_administrative_chapitre_imprimer_vew',$data);  // send the result from the model to the view 
   }
		
	}
	
	
